<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

include ('general.php');

function validExercice($id) {
    $row = executeRow("select * from Ejercicios where idEjercicio=$id");
    $mytime = time();
    $minTime = strtotime($row['FechaIni']);
    $maxTime = strtotime($row['FechaFin']);
    if ($row=="" || $mytime < $minTime || $mytime > $maxTime)
        return false;
    return true;
}

function validSubmission($idEj, $idProb) {
    if (!validExercice($idEj))
        return false;
    $sql = "select count(*) from EjercicioProblema where idEjercicio=$idEj and idProblema='$idProb'";
    return (executeScalar($sql)==1);
}

function getSolved($userId) {
    $sql = "select tc.problemId from TestCases tc inner join Envios e on
tc.tcaseId=e.tcaseId where e.idUsuario='$userId' and e.Veredict='YES';";
    return executeArray($sql);
}

function forceDownload($file) {
    if (file_exists($file)) {
        header("Content-type: application/force-download");
        header("Content-Transfer-Encoding: Binary");
        header("Content-length: ".filesize($file));
        header("Content-disposition: attachment; filename=\"".basename($file)."\"");
        readfile($file);
        exit;
    } else {
        echo "Archivo no existe";
    }
}

function newSubmission($idEj, $idProb, $userId) {
    $usedTC = executeArray("select tc.tcaseId from Envios e inner join TestCases tc on
e.tcaseId=tc.tcaseId where idUsuario='$userId' and problemId='$idProb'");
    $allTC = executeArray("select tcaseId from TestCases where problemId='$idProb'");
    $posibles = array_diff($allTC, $usedTC);
    $n = count($posibles);
    if ($n == 0) {
        $tcId = $allTC[rand(0, count($allTC)-1)];
    }
    else {
        $temp = array_values($posibles);
        $tcId = $temp[rand(0, $n - 1)];
    }
    $inFile = executeScalar("select tc.inFile from TestCases tc where tcaseId=$tcId");
    $fname = $inFile;
    
    //store submission
    $userId = $_SESSION['username'];
    $fechaIni = date( 'Y-m-d H:i:s', time() );
    $sql = "insert into Envios (idUsuario,HoraIni,tcaseId,Veredict) values ('$userId',
    '$fechaIni',$tcId,'TL');";
    //$idEnvio = 0;
    $idEnvio = executeNonQuery($sql, 1);    
    $_SESSION['idEnvio'] = $idEnvio;
        
    return array( $idEnvio, $fname );
}

function judgeSubmission($idEnvio, $userFile, $srcFile, $outFile) {
    $fechaFin = date( 'Y-m-d H:i:s', time() );
    $userOutFile = "protected/solutions/o$idEnvio.out";
    $userSrcFile = "protected/solutions/s$idEnvio".$srcFile['name'];
    move_uploaded_file($userFile["tmp_name"], $userOutFile);
    move_uploaded_file($srcFile["tmp_name"], $userSrcFile);
    $ans = exec("protected/judge.sh $userOutFile protected/inout/$outFile");
    $sql = "update Envios set HoraFin='$fechaFin', SourceFile='$userSrcFile', veredict='$ans' where idEnvio=$idEnvio";
    executeNonQuery($sql);
    return $ans;
}

function isGuessing($idProb, $fireGuessing) {
    if ( isset($_SESSION['idEnvio']) ) {
        $idEnvio = $_SESSION['idEnvio'];
        $row = executeRow("select HoraIni,Veredict from Envios where idEnvio=$idEnvio");
        $subTime = $row[0]; $veredict = $row[1];
        if ( strcmp($veredict,"YES") == 0 ) return 0;
        $subTime = strtotime($subTime);
        $dif = $fireGuessing - (time() - $subTime);
        if ($dif <= 0) return 0;
        else return $dif;
    }
    return 0;
}

?>
